package hh.cloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class UserController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    LoadBalancerClient loadBalancerClient;


    @Bean
    @LoadBalanced
    public RestTemplate template() {
        return new RestTemplate();
    }

    @GetMapping("/user/{id}")
    public Object findById(String id) {
        //组装用户下单信息

        ServiceInstance orderservice = loadBalancerClient.choose("orderservice");
        String host = orderservice.getHost();
        int port = orderservice.getPort();

        String url = "http://" + host + ":" + port + "/orders";

        url = "http://orderservice/orders";

        System.out.println("choose :" + url);
        String orders = restTemplate.getForObject(url, String.class);

        return "user and " + restTemplate;
    }
}
